﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Data;
using ReflectionStudio.Core.Events;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// 按照采购单对销售单完成留位
    /// 如果一个采购单被多个销售单预留，则逐个完成留位
    /// 按照逻辑，销售单需要先创建完成发货单，并指明发货单数量，默认就是销售单数量，
    /// 默认创建销售单时，就直接产生发货单
    /// </summary>
    public partial class TEKWZVK09 : UIWBBST
    {
        #region ==EKPMU===========
        public static readonly DependencyProperty EKPMUProperty = DependencyProperty.Register("EKPMU",
            typeof(string), typeof(TEKWZVK09),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEKPMUChanged));
        public string EKPMU
        {
            get
            {
                return (string)GetValue(EKPMUProperty);
            }
            set
            {
                SetValue(EKPMUProperty, value);
                LOADBYEKPMU(value);
            }
        }
        private static void OnEKPMUChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TEKWZVK09;
            if (src != null)
            {
                src.EKPMU = (string)e.NewValue;
            }
        }
        #endregion
         
        public TEKWZVK09()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();
        }
        ITXEXRPT ITXEXRPT;

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            if (IsFreeWB)
            {
                cvMaster.CustomFilter = "WBBAUFLFD='000000'";
            }
        }

        bool IsWB61 = false;
        bool IsWB62 = false;
      
        public void LOADBYEKPMU(string value)
        {
            try
            {
                TBMain.Clear();
                if (value.Length < 24)
                    return;
                LOADWBBYEKP(value);
                ALLOCATE2VKP(value);
                RWAUF = "";
                RWAUF = dwMaster["WBBRWKRS"].ToString() + dwMaster["WBBRWLFD"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void LOADWBBYEKP(string value)
        {
            dwData = TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBAUFTYP"] = value.Substring(0, 1);
            dwData["WBBAUFKRS"] = value.Substring(1, 2);
            dwData["WBBAUFLFD"] = value.Substring(3, 6);
            dwData["WBBAUFPOS"] = value.Substring(9, 3);
            SResult = TMV.QueryWBBST(dwData, 18);
            MergeData(SResult, true, true);
            vwMaster.View.MoveCurrentToFirst();
          
        }

        public void ALLOCATE2VKP(string value)
        {
            AUFTYP = value.Substring(16, 1);
            KRS = value.Substring(17, 2);
            LFD = value.Substring(19, 6);
            POS = value.Substring(25, 3);
            int idxd = value.IndexOf(",");
            int idxs = value.IndexOf(";");
            if (idxd < 0)
                return;
            if (idxs < 0)
                return;
            TargetMTR =0.001*Convert.ToDouble(value.Substring(idxd+1, idxs-1 - idxd));
            if (TargetMTR == 0)
                return;
            IsWB62 = false;
            IsWB61 = true;
            WBROWS = new List<DataRow>();
            DataTable TB = TBMain.Clone();
            int total = TBMain.Rows.Count;
            int processed = 0;
            foreach (DataRow _dwWB in TBMain.Rows)
            {
                processed++;
                if (_dwWB["WBBAUFLFD"].ToString() != "000000")
                {
                 
                    continue;
                }
                TargetMTR -= 0.001 * Convert.ToDouble(_dwWB["WBBISTKLG"].ToString());//先扣减，再留位，处理临界值
                if (TargetMTR <0)
                {
                    if (total - processed > 1)//如果是最后一个，则留位
                    {
                        TargetMTR += 0.001 * Convert.ToDouble(_dwWB["WBBISTKLG"].ToString());
                        break;
                    }
                }
                dwData = TB.Rows.Add();
                dwData.ItemArray = _dwWB.ItemArray;
                dwData["WBBAUFTYP"] = AUFTYP;
                dwData["WBBAUFKRS"] = KRS;
                dwData["WBBAUFLFD"] = LFD;
                dwData["WBBAUFPOS"] = POS;
                dwData["WBBTP"] = "0001";
                dwData["WBBAENDBEN"] = app.User;
                dwData["WBBAENDDAT"] = MV.date;
            }
            TargetMTR = TargetMTR * 1000;
            if (TargetMTR < 0)
                TargetMTR = 0;
            PM["ABARESMG"] =Helpers.Helper.RoundX(TargetMTR,1);
            PM["ABAAUF"] = value.Substring(0, 16);
            TMV.MUX.REDUCEMUQTY(PM);
            foreach (DataRow _dwWB in TB.Rows)
            {
                WBROWS.Add(_dwWB);
            }
            TMV.AUTOWB61(TMV.IsFreeWB);
        }  
    }
}
